import json
from typing import List, Dict, Any
from classes.PageElement import PageElement


class TestStep:
    """测试步骤，包含操作和相关元素"""
    _seq_counter = 1  # 类变量用于自动递增seq

    def __init__(self, desc: str, action: str, element: PageElement = None, **kwargs):
        self.desc = desc
        self.action = action
        self.element = element
        self.seq = TestStep._seq_counter
        TestStep._seq_counter += 1
        self.kwargs = kwargs


    def to_dict(self):
        step = {
            "seq": self.seq,
            "action": self.action,
            "desc": self.desc
        }
        # 支持 element 为空时直接通过 kwargs 传递 locator
        if self.element:
            step["locator"] = self.element.locator
        elif "locator" in self.kwargs:
            step["locator"] = self.kwargs["locator"]
        # 其它参数
        for k, v in self.kwargs.items():
            if k not in step:
                step[k] = v
        return step